# /**
# * Copyright (c) 2022 Beijing Jiaotong University
# * PhotLab is licensed under [Open Source License].
# * You can use this software according to the terms and conditions of the [Open Source License].
# *
# * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
# *
# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# *
# * See the [Open Source License] for more details.
# */
# /**
# * Author: Shihao Xie
# * Created: Jul. 8, 2022
# * Supported by: National Key Research and Development Program of China
# */

from phot_server.core.api_injector import api
from phot_server.manager.library_manager import LibraryManager
from phot_server.rpc.library_service.Library import Processor


# gettext instructions
# D:/Python/Tools/i18n/
# pygettext.py -d library -o locales/library.pot service/library_service.py
# msgfmt.py -o ./locales/cn/LC_MESSAGES/library.mo ./locales/cn/LC_MESSAGES/library
# msgfmt.py -o ./locales/en/LC_MESSAGES/library.mo ./locales/en/LC_MESSAGES/library

class LibraryService:
    """
    该服务是组件库相关服务，可以通过这个服务访问组件库和获取组件资源。
    调用该服务前，请确认已经做过连接服务相关操作并成功连接。例如：

    Examples:
        >>> from thrift import Thrift
        >>> from thrift.transport import TSocket
        >>> from thrift.transport import TTransport
        >>> from thrift.protocol import TBinaryProtocol
        >>> from photlab.library_service.Library import Client as library_client
        >>> transport = TSocket.TSocket("localhost", 8080)
        >>> transport_protocol = TTransport.TBufferedTransport(transport)
        >>> protocol_factory = TBinaryProtocol.TBinaryProtocol(transport_protocol)
        >>> library_client = library_client(protocol_factory)
        >>> transport.open()

    具体使用可以查看测试用例 test/test_interface/test_library.py
    """

    def __init__(self):
        self.library_manager = LibraryManager()

    @api(Processor.process_get_library_list)
    def get_library_list(self, path) -> (str, int):
        """
        获取组件库列表。

        Args:
            path (string): 组件库的相对路径，默认为"./"

        Returns:
            list<PhotFile>: 文件列表

        Raises:
            PhotError: 异常代码查看PhotError的exception_code

        Examples:
            >>> library_client.get_library_list("./")
        """
        return self.library_manager.get_library_list(path)

    @api(Processor.process_get_component_prototype)
    def get_component_prototype(self, path):
        """
        获取一个组件原型。

        Args:
            path (string): 组件在组件库的相对路径。

        Returns:
            PhotComponentPrototype: 组件原型

        Raises:
            PhotError: 异常代码查看PhotError的exception_code

        Examples:
            >>> library_client.get_component_prototype("./fiber.phtc")
        """
        return self.library_manager.get_component_prototype(path)

    @api(Processor.process_save_component_prototype)
    def save_component_prototype(self, path, component):
        """
        保存一个组件原型。

        Args:
            path (string): 组件在组件库的相对路径。
            component (PhotComponentPrototype): 组件原型

        Returns:
            无

        Raises:
            PhotError: 异常代码查看PhotError的exception_code

        Examples:
            >>> component = PhotComponentPrototype(name="library_test_case1",
                                                   calculationMethod="test",
                                                   parallelComputingMethod="CPU",
                                                   isAnalyzer="False",
                                                   isClass="False",
                                                   description="Component for test. It is not real.",
                                                   category="./",
                                                   path="./test_component.phtc",
                                                   parameters=[PhotParameter(name="num_bits",
                                                                             dataType="int",
                                                                             value="393216",
                                                                             unit="bit",
                                                                             photUnit="bit",
                                                                             nameTag="Number of bits",
                                                                             interval="(0,inf)",
                                                                             category="/",
                                                                             description="Number of bits"
                                                                             )],
                                                   rect=RenderRect(x=10, y=120, width=10, height=10),
                                                   ports=[RenderPort(portId=0,
                                                                     direction=PortDirection.OUT,
                                                                     aspect=PortAspect.RIGHT,
                                                                     location=0,
                                                                     name="Output signal",
                                                                     description="Output a binary signal.")]
                                                   )
            >>> library_client.save_component_prototype("./test_component.phtc", component)
        """
        self.library_manager.save_component_prototype(path, component)

    @api(Processor.process_delete_component_prototype)
    def delete_component_prototype(self, path):
        """
        删除组件原型。

        Args:
            path (string): 组件在组件库的相对路径。

        Returns:
            无

        Raises:
            PhotError: 异常代码查看PhotError的exception_code

        Examples:
            >>> library_client.delete_component_prototype("./test_component.phtc")
        """
        self.library_manager.delete_component_prototype(path)

    @api(Processor.process_search_component)
    def search_component(self, query_string):
        """
        搜索组件，搜索范围为组件名称(name)和组件描述(description)。暂时不支持其他搜索功能。

        Args:
            query_string (string): 查询语句

        Returns:
            list<PhotComponentPrototype>: 组件原型列表

        Raises:
            PhotError: 异常代码查看PhotError的exception_code

        Examples:
            >>> library_client.search_component("fiber")
        """
        return self.library_manager.search_component(query_string)
